$(function () {

    //全选事件
    $("#checkAll").click(function () {
        var text = $("#checkAll").text();
        if(text=="全选"){
            $('#listTable').bootstrapTable('checkAll');
            $("#checkAll").text("取消全选");}
        if(text=="取消全选"){
            $('#listTable').bootstrapTable('uncheckAll');
            $("#checkAll").text("全选");}
    })
    //单击状态事件
    $(".status").click(function () {
        $(this).addClass('btn-primary').siblings().removeClass('btn-primary');
        statusGlobal = $(this).val();
        CommonUtils.query();
        // CommonUtils.query();
    })
    //单击等级事件
    $(".level").click(function () {

        $(this).addClass('btn-primary').siblings().removeClass('btn-primary');
        levelGlobal = $(this).val();
        CommonUtils.query();
    })
    //批量处理事件
    $(".patch").click(function () {
        //获取所有已选择的列
      var checkedList =  $('#listTable').bootstrapTable('getAllSelections');
      var checkedLevel = $(this).val();
      var data_list = new Array();
      if(checkedList.length == 0){
          layer.msg('未选择数据');

      }else{
          $.each(checkedList,function (n,value) {
              console.info(value.id);
          data_list.push(value.id);
             });
          if(checkedLevel === "0"){
              var message ="是否确认选中部分全部修保持现有等级级?"
          }else{
               message ="是否确认选中部分全部修改为" + checkedLevel + "级?"
          }

          var data ={
              "censorLevel":checkedLevel,
              "list":data_list
          }
          layer.confirm(
              message,
              {icon: 3, title: '提示'},
              function(index){
                  //alert("确认全部修改"+indexGlobal)
                  $.ajax({
                      type: "post",
                      url: "/censor/updateBatch",
                      dataType: "json",
                      contentType : 'application/json',
                      data: JSON.stringify(data),
                      success:function(result){
                          if (result.code === '0') {
                              $('#listTable').bootstrapTable('refresh');
                          } else {
                              layer.alert("操作失败", {icon: 5, time: 2000, title: '提示'});
                          }
                          $("#checkAll").text("全选");
                      }
                  });
                   layer.close(index);
                   //取消选择数据
              },
              function (index) {
                 // alert("确认取消全部修改"+indexGlobal);

                  layer.close(index);
              }
          );


      }
    })
    //单击号码显示事件
    $('#listTable').on("click-cell.bs.table", function ($event, field, value, row, target) {

        var index = target[0].parentElement.rowIndex;
        index = index - 1;
        var phone = row.senderPhone
        indexGlobal = index
        objGlobal = row;
        if (field == "senderPhoneEcrypted") {
            $('#listTable').bootstrapTable('updateCell', {
                index: index,  //更新列所在行的索引
                field: field, //要更新列的field
                value: phone //要更新列的数据
            })
        }
        return false;
    });

    //单条记录更新
    $("body").on("click", ".action", function () {
        var level = $(this).val();
        console.info($(this).val());
       // var obj = $('#listTable').bootstrapTable('getSelections');
        if (null != objGlobal) {
            var id = objGlobal.id;
            //var censorCode = obj[0].censorCode;
            console.info(JSON.stringify(objGlobal));
            layer.confirm(
                "确认修改等级为" + level + "级吗?",
                {icon: 3, title: '提示'},
                function (index) {
                    //alert("success"+indexGlobal)

                    var data_list = new Array();
                    data_list.push(id);
                    var data ={
                        "censorLevel":level,
                        "list":data_list
                    }
                    $.ajax({
                        type: "post",
                        url: "/censor/updateBatch",
                        dataType: "json",
                        contentType : 'application/json',
                        data: JSON.stringify(data),
                        success: function (result){
                            if (result.code === '0') {
                                layer.msg('审核成功');
                                $('#listTable').bootstrapTable('refresh');

                            } else {
                                layer.alert("操作失败", {icon: 5, time: 2000, title: '提示'});
                            }
                        },
                        error: function (result) {
                            layer.alert("未知错误,请刷新数据查看", {icon: 0, time: 1000, title: '提示'});
                        }
                    });
                    $("#checkAll").text("全选");
                    $('#listTable').bootstrapTable('uncheck', indexGlobal);
                    layer.close(index);

                },
                function (index) {
                    //alert("cancle"+indexGlobal)
                    $('#listTable').bootstrapTable('uncheck', indexGlobal);
                    layer.close(index);

                }
            )
        }

    })


})